package com.cgy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cgy.entity.Keshi;
import com.cgy.entity.Teacher;
import com.cgy.service.TeacherService;

import util.LayuiJsonUtils;

@RestController
@RequestMapping("teacher")
@CrossOrigin
public class TeacherController {
	@Autowired
	private TeacherService teaService;

	@GetMapping
	public LayuiJsonUtils<Teacher> findByNamePage(LayuiJsonUtils<Teacher> lj, Teacher t) {
		Pageable pageable = PageRequest.of(lj.getPage(), lj.getLimit());
		Page<Teacher> tea = teaService.findBytnamePage(t, pageable);
		lj.setData(tea.getContent());
		lj.setCount(tea.getTotalElements());
		return lj;

	}
	
	/**
	 * 添加
	 */
	@PostMapping
	public Teacher addAndUpdateteacher(Teacher t,Integer kid) {
		Keshi k = new Keshi();
		k.setKid(kid);
		t.setKeshi(k);
		return teaService.addAndUpdateTeacher(t);
	}
	/**
	 * 修改
	 */
	@PutMapping
	public Teacher Updateteacher(Teacher t,Integer kid) {
		Keshi k = new Keshi();
		k.setKid(kid);
		t.setKeshi(k);
		return teaService.addAndUpdateTeacher(t);
	}
	/**
	 * 删除
	 * 
	 * @param tid
	 * @return
	 */
	@DeleteMapping("{tid}")
	public Integer deleteStudentByID(@PathVariable("tid") Integer tid) {
		return  teaService.deleteTeacher(tid);
	}
		 
}
